﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Pheonix.CodeLibrary.Web;
using Pheonix.Fashionzoomer.BLL;
using Pheonix.Fashionzoomer.Entity.SqlServer2000;
using System.Collections.Generic;

public partial class page_public_shoplist : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Title = SiteName + " - Shop Search"; 
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        LoadData();
    }

    public string GetRatingImagePath(object ratingPoint)
    {
        decimal rating = (decimal)ratingPoint;
        if (rating < 2)
            rating = 2;
        return "~/image/star" + Convert.ToInt32(Math.Round(Convert.ToDouble(rating))).ToString() +".jpg";
    }

    private void LoadData()
    {
        int CurrentPage = dataPager.CurrentPage;
        string keywords, category, brand, searchType, shopId;

        keywords = Request.QueryString["keywords"];
        category = Request.QueryString["category"];
        brand = Request.QueryString["brand"];
        shopId = Request.QueryString["shopId"];
        searchType = Request.QueryString["searchType"];

        string brandValue = (String.IsNullOrEmpty(brand)) ? (String.Empty) : brand;
        int categoryValue = (String.IsNullOrEmpty(category)) ? (0) : (Int32.Parse(category));
        string shopIdValue = (String.IsNullOrEmpty(shopId)) ? (String.Empty) : shopId;
        string keywordsValue = (String.IsNullOrEmpty(keywords)) ? (String.Empty) : (keywords.Replace(" ", ","));
        SearchType searchTypeValue = (String.IsNullOrEmpty(searchType)) ? (SearchType.ShopSearch) : ((SearchType)(Int32.Parse(searchType)));

        List<Shop> searchResult;
        searchResult = ShopManager.GetShopSearchResult(brandValue, shopIdValue, categoryValue, keywordsValue);

        PagedDataSource pagedDataSource = new PagedDataSource();
        pagedDataSource.AllowPaging = true;
        pagedDataSource.DataSource = searchResult;
        pagedDataSource.PageSize = dataPager.PageSize;
        pagedDataSource.CurrentPageIndex = CurrentPage - 1;

        dataPager.PrevPageEnabled = !pagedDataSource.IsFirstPage;
        dataPager.NextPageEnabled = !pagedDataSource.IsLastPage;

        if (dataPager.PrevPageEnabled == dataPager.NextPageEnabled && CurrentPage == 0)
            dataPager.Visible = false;

        dataPager.LastPage = pagedDataSource.PageCount;

        //if (!IsPostBack)
        //{
            dataPager.SetPageLinks();
        //}

        shopList.DataSource = pagedDataSource;
        shopList.DataBind();

    }

    protected void ShopListDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;
        if ((item.ItemType == ListItemType.Item) ||
            (item.ItemType == ListItemType.AlternatingItem))
        {
            Repeater shopCategoryList = (Repeater)item.FindControl("shopTopCategories");
            Shop shop = (Shop)e.Item.DataItem;
            shopCategoryList.DataSource = ShopManager.GetShopCategoryList(shop.Id);
            shopCategoryList.DataBind();

            HtmlImage GotoShop = (HtmlImage)item.FindControl("GotoShop");
            GotoShop.Src = "~/App_Themes/" + Page.StyleSheetTheme + "/image/btnGotoShop.jpg"; ;
        }
    }

    protected void GotoShop(object sender, CommandEventArgs e)
    {
        Response.Redirect("shopdetail.aspx?id=" + Convert.ToInt32(e.CommandArgument));
    }

    protected void GotoShopClick(object sender, EventArgs e)
    {

    }
}
